package com.lingchen.boot.utils;

import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.ToLongFunction;

public class ListUtils {

	public static <T> List<T> rankLong(List<T> items, ToLongFunction<T> valueOffer,
			BiConsumer<Integer, T> rankConsumer) {
		items.sort((o1, o2) -> Long.valueOf(valueOffer.applyAsLong(o2))
				.compareTo(Long.valueOf(valueOffer.applyAsLong(o1))));
		long lastCount = -1;
		int rank = 1;
		int i = 0;
		for (T item : items) {
			long value = valueOffer.applyAsLong(item);

			if (i > 0 && value < lastCount) {
				rank = i + 1;
			}

			rankConsumer.accept(rank, item);

			lastCount = value;
			i++;
		}
		return items;
	}
}
